﻿using OpenSage.Content;
using OpenSage.Data.Ini;

namespace OpenSage.Logic.Object;

internal sealed class UnpauseSpecialPowerUpgrade : UpgradeModule
{
    private readonly UnpauseSpecialPowerUpgradeModuleData _moduleData;

    internal UnpauseSpecialPowerUpgrade(GameObject gameObject, IGameEngine gameEngine, UnpauseSpecialPowerUpgradeModuleData moduleData)
        : base(gameObject, gameEngine, moduleData)
    {
        _moduleData = moduleData;
    }

    protected override void OnUpgrade()
    {
        var powerToUnpause = _moduleData.SpecialPowerTemplate.Value;
        foreach (var specialPowerModule in GameObject.FindBehaviors<SpecialPowerModule>())
        {
            if (specialPowerModule.Matches(powerToUnpause))
            {
                specialPowerModule.Unpause();
            }
        }
    }

    internal override void Load(StatePersister reader)
    {
        reader.PersistVersion(1);

        reader.BeginObject("Base");
        base.Load(reader);
        reader.EndObject();
    }
}

public sealed class UnpauseSpecialPowerUpgradeModuleData : UpgradeModuleData
{
    internal static UnpauseSpecialPowerUpgradeModuleData Parse(IniParser parser) => parser.ParseBlock(FieldParseTable);

    private static new readonly IniParseTable<UnpauseSpecialPowerUpgradeModuleData> FieldParseTable = UpgradeModuleData.FieldParseTable
        .Concat(new IniParseTable<UnpauseSpecialPowerUpgradeModuleData>
        {
            { "SpecialPowerTemplate", (parser, x) => x.SpecialPowerTemplate = parser.ParseSpecialPowerReference() },
            { "ObeyRechageOnTrigger", (parser, x) => x.ObeyRechageOnTrigger = parser.ParseBoolean() },
        });

    public LazyAssetReference<SpecialPower> SpecialPowerTemplate { get; private set; }

    [AddedIn(SageGame.Bfme2)]
    public bool ObeyRechageOnTrigger { get; private set; }

    internal override BehaviorModule CreateModule(GameObject gameObject, IGameEngine gameEngine)
    {
        return new UnpauseSpecialPowerUpgrade(gameObject, gameEngine, this);
    }
}
